<%--
  Created by IntelliJ IDEA.
  User: zhoushuang
  Date: 2025/6/11
  Time: 上午10:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form>
    <label for="name">用户名:</label> <input type="text" id="name" name="name"><br>
    <label for="password">密码:</label> <input type="password" id="password" name="password"><br>
    <input type="text" hidden="hidden" name="opr" value="login">
    <input type="button" value="登录">
</form>
<script>
    document.querySelector('input[type="button"]').addEventListener('click', function (event) {
        event.preventDefault();
        let name = document.querySelector('#name').value;
        let password = document.querySelector('#password').value;
        let opr = document.querySelector('input[name="opr"]').value;
        var xhr = new XMLHttpRequest();
        xhr.open('POST', '${pageContext.request.contextPath}/login');
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xhr.send('name=' + name + '&password=' + password + '&opr=' + opr);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    alert(xhr.responseText);
                    window.location.href = '${pageContext.request.contextPath}/logout.jsp';
                } else {
                    alert('登录失败，请检查用户名和密码');
                }
            }
        }
    })
</script>
</body>
</html>
